﻿Imports System.Data.OleDb
Public Class CoinDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then

            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
            conn.Open()
            Dim LikedorNotsql As String = "SELECT * FROM CoinsLike WHERE CoinsImageLike = @f1 AND Reader = @f2"
            Dim LikedorNotcmd As New OleDbCommand(LikedorNotsql, conn)
            LikedorNotcmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            LikedorNotcmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            Dim dr As OleDbDataReader = LikedorNotcmd.ExecuteReader()
            If (dr.HasRows()) Then
                Btn_like.Visible = False
                lbl_like.Visible = True
                img_like.Visible = True
            Else
                Btn_like.Visible = True
                lbl_like.Visible = False
                img_like.Visible = False
            End If
            Dim LikeCountsql As String = "SELECT * FROM CoinLikeCount WHERE CoinsImageLike=@f30"
            Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
            conn2.Open()
            Dim LikeCountcmd As OleDbCommand = New OleDbCommand(LikeCountsql, conn2)
            LikeCountcmd.Parameters.AddWithValue("@f30", Request.Params("ID"))
            Dim datareader = LikeCountcmd.ExecuteReader()
            datareader.Read()
            If (datareader.HasRows) Then
                lbl_ImageCounts.Text = datareader("UserLikes")
            Else
                lbl_ImageCounts.Text = 0
            End If
        End If
        Dim Ratingconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        Dim Ratings As Single = 0
        If Not IsPostBack Then
            BindAverageRatingControl()
        End If
        Dim PageViewssql As String = "INSERT INTO CoinPageViews (DiscussionID,DiscussionViewer) VALUES (@f1,@f2)"
        Dim PageViewsconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        PageViewsconn.Open()
        Dim PageViewscmd As OleDbCommand = New OleDbCommand(PageViewssql, PageViewsconn)
        PageViewscmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        PageViewscmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        PageViewscmd.ExecuteNonQuery()
        PageViewsconn.Close()
        PageViewscmd.Dispose()
        PageViewsconn.Dispose()

        Dim PageViewCountsql As String = "SELECT * FROM PageViewQuery WHERE DiscussionID=@f20"
        Dim PageViewCountconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        PageViewCountconn.Open()
        Dim PageViewCountcmd As OleDbCommand = New OleDbCommand(PageViewCountsql, PageViewCountconn)
        PageViewCountcmd.Parameters.AddWithValue("@f20", Request.Params("ID"))
        Dim datapagereader = PageViewCountcmd.ExecuteReader()
        datapagereader.Read()
        If (datapagereader.HasRows) Then
            lbl_PageViews.Text = datapagereader("CountOfPageViews")
        Else
            lbl_PageViews.Text = 0
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        Dim sql = "INSERT INTO Comments(Author,Subject,Message,CoinsID) VALUES (@f1,@f2,@f3,@f4)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_subject.Text)
        cmd.Parameters.AddWithValue("@f3", tb_message.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("CoinDetail.aspx?ID=" & Request.Params("ID"))
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim CoinsID = Request.Params("ID")
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        conn.Open()


        Dim newTags = tb_newTags.Text.Split(",")
        For Each newTag As String In newTags
            Dim isTagthereCmd As New OleDbCommand("SELECT * FROM  Tags WHERE Tag=@f1", conn)
            isTagthereCmd.Parameters.AddWithValue("@f1", newTag)
            Dim dr = isTagthereCmd.ExecuteReader()
            If Not dr.HasRows Then
                Dim addTagCmd As New OleDbCommand("INSERT INTO Tags (Tag) VALUES(@f1)", conn)
                addTagCmd.Parameters.AddWithValue("@f1", newTag)
                addTagCmd.ExecuteNonQuery()
            End If
            Dim findNewTagIDCmd As New OleDbCommand("SELECT ID FROM  Tags WHERE Tag=@f1", conn)
            findNewTagIDCmd.Parameters.AddWithValue("@f1", newTag)
            Dim newTagdr = findNewTagIDCmd.ExecuteReader()
            newTagdr.Read()
            Dim newID = newTagdr.Item("ID")
            Dim makeAssociationcmd As New OleDbCommand("INSERT INTO CoinsTagJunction (TagsfkID,CoinsfkID) VALUES (@f1,@f2)", conn)
            makeAssociationcmd.Parameters.AddWithValue("@f1", newID)
            makeAssociationcmd.Parameters.AddWithValue("@f2", CoinsID)
            makeAssociationcmd.ExecuteNonQuery()


        Next
        For Each li As ListItem In CheckBoxList1.Items
            If (li.Selected = True) Then
                Dim sql = "INSERT INTO CoinsTagJunction (TagsfkID,CoinsfkID) VALUES (@f1,@f2)"
                Dim cmd As New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", li.Value)
                cmd.Parameters.AddWithValue("@f2", CoinsID)
                cmd.ExecuteNonQuery()
            End If
        Next
        conn.Close()
    End Sub

    Protected Sub GridView2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView2.SelectedIndexChanged
       
    End Sub

    Protected Sub Btn_like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_like.Click
        
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        conn.Open()
        Dim likesql As String = "INSERT INTO CoinsLike (CoinsImageLike,Reader) VALUES (@f1,@f2)"
        Dim likecmd As New OleDbCommand(likesql, conn)

        likecmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        likecmd.Parameters.AddWithValue("@f2", User.Identity.Name)



        likecmd.ExecuteNonQuery()
        Btn_like.Visible = False
        lbl_like.Visible = True
        img_like.Visible = True
        conn.Close()


        Response.Redirect("CoinDetail.aspx?ID=" & Request.QueryString("ID"))
    End Sub


    Public Sub RatingControlChanged(ByVal sender As Object, ByVal e As AjaxControlToolkit.RatingEventArgs)
        Dim CoinID = Request.Params("ID")
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("Coinsconn").ConnectionString)
        Dim Ratingssql As String = "DELETE FROM UserRating WHERE Rater=@f1 and CoinfkID=@f2"
        Dim RatingCmd As New OleDbCommand(Ratingssql, conn)
        conn.Open()
        RatingCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        RatingCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        RatingCmd.ExecuteNonQuery()
        conn.Close()
        Ratingssql = "INSERT INTO UserRating(Rating,Rater,CoinfkID) VALUES (@f1,@f2,@f3)"
        Dim addRatingCmd As New OleDbCommand(Ratingssql, conn)
        conn.Open()
        addRatingCmd.Parameters.AddWithValue("@f1", Ratn_Star.CurrentRating)
        addRatingCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addRatingCmd.Parameters.AddWithValue("@f3", Request.Params("ID"))
        addRatingCmd.ExecuteNonQuery()
        conn.Close()
    End Sub

    Public Sub BindAverageRatingControl()
        Dim Ratingconn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("CoinsConn").ConnectionString)
        Dim CoinID = Request.Params("ID")
        Dim Ratings As Single = 0
        Dim DtRating As DataTable = New DataTable()
        Ratingconn.Open()
        Dim Ratingcmd As OleDbCommand = New OleDbCommand("SELECT UserRating.CoinfkID, AVG(Rating) AS AverageRating FROM(UserRating) WHERE CoinfkID =" & CoinID & "  GROUP BY CoinfkID", Ratingconn)
        Dim DataAdap As OleDbDataAdapter = New OleDbDataAdapter(Ratingcmd)
        DataAdap.Fill(DtRating)

        If DtRating.Rows.Count <> 0 Then
            Ratings = Single.Parse(DtRating.Rows(0)("AverageRating").ToString())
            Ratn_avg.CurrentRating = Convert.ToInt32(Ratings)
        Else
            Ratn_avg.CurrentRating = 0
        End If
        lbl_MyRating.Text = Ratings.ToString()

    End Sub
End Class